package com.example.wechatscancode.utils;


import com.google.gson.GsonBuilder;

import java.util.regex.Pattern;


/**
 * 实现 bean To json 相互转换
 */
public class JsonSeriaUtil {


    public static String beanToJson(Object obj) {
        String json = null;
        try {
            json = Pattern.compile("\\s{2,}|\t|\r|\n").matcher(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(obj)).replaceAll("");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    public static <T> T beanFromJson(String xml, Class<T> clazz) {
        try {
            return (T) new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().fromJson(xml, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getHourMinS(int num) {
        String mHour;
        String mMinute;
        String mSecond;
        int hour = num / 3600;
        int minute = num % 3600 / 60;
        int second = num % 3600 % 60;
        // 时
        if (hour > 0 && hour < 10) {
            mHour = "0" + hour;
        } else if (hour >= 10) {
            mHour = hour + "";
        } else {
            mHour = "00";
        }
        // 分
        if (minute > 0 && minute < 10) {
            mMinute = "0" + minute;
        } else if (minute >= 10) {
            mMinute = minute + "";
        } else {
            mMinute = "00";
        }
        // 秒
        if (second > 0 && second < 10) {
            mSecond = "0" + second;
        } else if (second >= 10) {
            mSecond = second + "";
        } else {
            mSecond = "00";
        }
        return mHour + ":" + mMinute + ":" + mSecond;
    }


}
